package com.achievement.controller;

import com.achievement.domain.dto.honor.HonorApplyBookDTO;
import com.achievement.domain.dto.honor.HonorDTO;
import com.achievement.domain.entity.HonorEntity;
import com.achievement.domain.param.honor.HonorApplyBookParam;
import com.achievement.domain.param.honor.HonorParam;
import com.achievement.domain.param.honor.ReviewPlanParam;
import com.achievement.domain.vo.honor.HonorApplyBookExcelVO;
import com.achievement.domain.vo.honor.HonorApplyBookReviewVO;
import com.achievement.domain.vo.honor.HonorExcelVO;
import com.achievement.domain.vo.honor.HonorVO;
import com.achievement.service.IHonorAuthorService;
import com.achievement.service.IHonorService;
import com.alibaba.fastjson2.JSONObject;
import com.common.annotation.Anonymous;
import com.common.annotation.Log;
import com.common.api.ApprovalApi;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.common.enums.BusinessType;
import com.common.utils.SecurityUtils;
import com.common.utils.StringUtils;
import com.common.utils.poi.ExcelUtil;
import com.flowable.service.IFlowTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "成果获奖-获奖成果")
@RestController
@RequestMapping("/achievement/honor")
public class HonorController extends BaseController {
    @Autowired
    private IHonorService honorService;

    @Autowired
    private IHonorAuthorService honorAuthorService;
    @Autowired
    private IFlowTaskService flowTaskService;
    @Autowired
    private ApprovalApi approvalApi;
    private final static String module_name = "获奖成果";
    @PreAuthorize("@ss.hasPermi('achievement:honor:list')")
    @ApiOperation("科研管理员-科研秘书 获取获奖成果列表")
    @GetMapping("/list")
    public TableDataInfo list(HonorParam param)
    {
        startPage();
        List<HonorVO> list = honorService.listByPage(param);
        //获取当前用户所有的代办列表
        Map<String, String> instanceTaskMap = approvalApi.getInstanceTaskMap(module_name);
        for (HonorVO u:list) {
            String processInstanceId = u.getProcessInstanceId();
            if (StringUtils.isNotEmpty(processInstanceId) && instanceTaskMap.containsKey(processInstanceId)) {
                u.setTaskId(instanceTaskMap.get(processInstanceId));
                u.setIsAuditor(flowTaskService.IsAuditor(SecurityUtils.getLoginUser().getUser().getUserId().toString(),u.getProcessInstanceId()));
            } else {
                u.setIsAuditor(false);
            }
        }
        return getDataTable(list);
    }
    /**
     * 左上方统计 全部 学校通过 学校退回 待审核
     * @param param 检索参数
     * @return AjaxResult
     */
    @PreAuthorize("@ss.hasPermi('achievement:honor:list-statistics')")
    @ApiOperation("管理员-获奖列表左上方分组")
    @GetMapping(value = "/list-statistics")
    public AjaxResult listStatistics(HonorParam param)
    {
        List<HashMap<String, Integer>> list =honorService.listStatistics(param);
        return success(list);
    }


    /**
     * 获奖成果导出
     */
    @PreAuthorize("@ss.hasPermi('achievement:honor:export')")
    @ApiOperation("获奖成果导出")
    @Log(title = "获奖成果导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody Map<String,Object> requestMap)
    {
        HonorParam param = JSONObject.parseObject(JSONObject.toJSONString(requestMap), HonorParam.class);
        List<String> selectedColumns = (List<String>) requestMap.get("selectedColumns");
        String[] fields = selectedColumns != null ? selectedColumns.toArray(new String[0]) : new String[0];
        List<HonorExcelVO> list = honorService.honorLisExcel(param);
        ExcelUtil<HonorExcelVO> util = new ExcelUtil<>(HonorExcelVO.class);
        util.showColumn(fields);
        util.exportExcelByOrder(response, list, "获奖成果列表",fields);
    }

    /**
     * 获取获奖成果详细信息
     */
    @PreAuthorize("@ss.hasPermi('achievement:honor:query')")
    @ApiOperation("获取获奖成果详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(honorService.getInfoById(id));
    }


    /**
     * 新增获奖成果
     */
    @PreAuthorize("@ss.hasPermi('achievement:honor:add')")
    @ApiOperation("新增获奖成果 管理员、科研人员 暂存，管理员保存提交")
    @Log(title = "新增获奖成果", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody HonorDTO dto)
    {
        return honorService.addHonorAndHonorAuthor(dto);
    }
    /**
     * 修改获奖成果
     */
    @PreAuthorize("@ss.hasPermi('achievement:honor:update')")
    @ApiOperation("修改获奖成果")
    @Log(title = "修改获奖成果", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/update")
    public AjaxResult edit(@RequestBody HonorDTO dto)
    {
        return honorService.updateHonorAndHonorAuthor(dto);
    }

    /**
     * 删除获奖成果
     */
    @PreAuthorize("@ss.hasPermi('achievement:honor:remove')")
    @ApiOperation("删除获奖成果")
    @Log(title = "删除获奖成果", businessType = BusinessType.DELETE)
    @DeleteMapping("/del/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(honorService.batchRemove(ids));
    }

}
